package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.utils.DateTimeUtils;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import com.yzkj.cpjxc.business.domain.JieZhang;
import com.yzkj.cpjxc.business.service.JieZhangService;
import com.yzkj.cpjxc.business.domain.PingZhengQueryVo;
import com.yzkj.cpjxc.business.service.PingZhengService;
import com.yzkj.cpjxc.business.domain.ZhangTao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 结账控制器
 */
@Controller
@RequestMapping("/jieZhang")
public class JieZhangController extends BaseController {

	@Autowired
	private JieZhangService jieZhangService;

	@Autowired
	private PingZhengService pingZhengService;

	@RequestMapping("/list")
	@DoLog(cnContent = "查看结账列表信息")
	@Permission(name="jieZhang.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, JieZhang jieZhang) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();

		PingZhengQueryVo queryVo = new PingZhengQueryVo();
		//当前账套账期
		JieZhang jz = (JieZhang) request.getSession().getAttribute(Constants.CURRENT_JIE_ZHANG);
		ZhangTao zt = (ZhangTao) request.getSession().getAttribute(Constants.CURRENT_ZHANG_TAO);
		queryVo.setCompanyId(zt.getUuid());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startTime = jz.getJzYear() + "-" + StringUtils.leftPad(jz.getJzMonth() + "", 2, "0") + "-01";

		Map<String, Date> datemap = DateTimeUtils.getMonthEndDateForStartDate(sdf.parse(startTime));
		queryVo.setStartTime(datemap.get(Constants.START_TIME));
		queryVo.setEndTime(datemap.get(Constants.END_TIME));

		Long countPz = pingZhengService.getPzCount(queryVo);
		modelMap.put("title", "共录入凭证 " + countPz + " 张，财务初始余额试算平衡");

		modelMap.put("jieZhang", jieZhang);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("jieZhang/list").addAllObjects(modelMap);
	}

	@RequestMapping("/toAdd")
	@DoLog(cnContent = "去到添加结账页面")
	@Permission(name="jieZhang.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, JieZhang jieZhang) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();

		//当前账套账期
		JieZhang jz = (JieZhang) request.getSession().getAttribute(Constants.CURRENT_JIE_ZHANG);
		//下一账期
		int year = jz.getJzYear();
		int month = jz.getJzMonth();
		if (month == 12) {
			year = year + 1;
			month = 1;
		} else {
			month = month + 1;
		}

		modelMap.put("year", year);
		modelMap.put("month", month);

		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("jieZhang/add").addAllObjects(modelMap);
	}

	@RequestMapping("/add")
	@DoLog(cnContent = "添加结账")
	@Permission(name="jieZhang.add")
	public String add(HttpServletRequest request, HttpServletResponse response, JieZhang jieZhang) throws Exception {
		try {
			
			//当前账期
			JieZhang jz = (JieZhang) request.getSession().getAttribute(Constants.CURRENT_JIE_ZHANG);
			//当前账套
			ZhangTao zt = (ZhangTao) request.getSession().getAttribute(Constants.CURRENT_ZHANG_TAO);
			jieZhang.setUuid(IDUtils.genUUID());
			jieZhang.setZhangTaoId(zt.getUuid());
			jieZhang.setIsYjz(Constants.IS_WJZ);

			int year = jz.getJzYear();
			int month = jz.getJzMonth();
			if (month == 12) {
				year = year + 1;
				month = 1;
			} else {
				month = month + 1;
			}
			int new_year = jieZhang.getJzYear();
			int new_month = jieZhang.getJzMonth();
			//防止提交数据被非法修改
			if (new_year != year || new_month != month) {
				return this.ajax(response, "不能非法修改下一账期时间！");
			}

			BusinessMap bm = jieZhangService.createJieZhang(jieZhang,jz,zt);

			if (bm.getIsSucc()) {
				//修改当前session账期
				JieZhang newjieZhang = jieZhangService.getByZhangTaoId(zt.getUuid());
				request.getSession().setAttribute(Constants.CURRENT_JIE_ZHANG, newjieZhang);
			}

			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

}
